import pygame
import sys
import tollgate, enemy, star, player, collidet
import weapon, flame, msg

pygame.init()

class game_obj:

    LEVEL_LIMIT = 2
    
    def __init__(self, screen, player_num):
        self.screen = screen
        self.screen_size = screen.get_size()
        self.player_num = player_num
        pass

    def run_game(self):
        is_running = True
        
        somebody_alive = True
        
        level = 0

        clock = pygame.time.Clock()
        enemys = pygame.sprite.Group()
        stars = pygame.sprite.Group()
        players = pygame.sprite.Group()
        shots = pygame.sprite.Group()
        enemy_shots= pygame.sprite.Group()
        gunfires = pygame.sprite.Group()
        exps = pygame.sprite.Group()
        bomb = pygame.sprite.Group()

        sprites = pygame.sprite.Group()
#        upper = pygame.sprite.Group()
#        lower = pygame.sprite.Group()

        enemy.enemy_obj.containers = enemys, sprites
        star.star_obj.containers = stars, sprites
        player.player_obj.containers = players, sprites
        weapon.shot_obj.containers = shots, sprites
        weapon.enemy_shot_obj.containers = enemy_shots, sprites
        weapon.gun_fire.containers = gunfires, sprites
        weapon.bomb_obj.containers = bomb, sprites
        flame.flame_spot.containers = exps, sprites

        for pn in range(self.player_num):
            #print pn + 1
            if (self.player_num == 1):
                #print ">> 1p"
                player.player_obj(pn + 1, self.screen_size, True)
            else:
                #print ">> 2p"
                player.player_obj(pn + 1, self.screen_size, False)

        for i in range(80):
            star.star_obj(self.screen_size)

        while(somebody_alive):

            current_frame = 0              #Current frame counter
            
            if (level < self.LEVEL_LIMIT):
                level += 1
            else:
                return


            # MAP FILE PROCCESSING
            
            cur_mp = tollgate.map_proccessor(self.screen_size, players, str(level) + ".map")

            boss_num = cur_mp.get_boss_num()

            print "New mission>> boss_num: ",boss_num

            ending_time = 50

            start_time = 50

            message = msg.msgimage("LEVEL %d READY" %(level), self.screen.get_rect().center, (248, 221, 28), 40)

            while((somebody_alive and boss_num) or ending_time):
                
                clock.tick(15.7)

                
                
                for event in pygame.event.get():
                    if (event.type == pygame.QUIT):
                        sys.exit()
                    if (event.type == pygame.KEYDOWN):
                        if (event.key == pygame.K_ESCAPE):
                            #print "Change game_pause"
                            is_running ^= True
                        elif (event.key == pygame.K_1):
                            #print "Kill boss"
                            boss_not_killed = False
                        elif (event.key == pygame.K_2):
                            #print "Kill all players"
                            somebody_alive = False
                        else:
                            pass

                if (is_running):

                    if (not start_time):
                        current_frame += 1
                    else:
                        start_time -= 1
                        pass
                    #print current_frame

                    while (1):    #read the object from the map file
                        if (cur_mp.get_available_obj(current_frame) == False):
                            break;
                    
                    self.screen.fill((0,0,0))

                    players.update()
                    enemys.update()
                    stars.update()
                    shots.update()
                    gunfires.update()
                    exps.update()
                    enemy_shots.update()
                    bomb.update()
                    
#                    sprites.update()

                    for p in players:
                        collidet_enemy = collidet.get_collidet_sg(p, enemys)
                        if (collidet_enemy):
                            if (p.is_alived() and not p.is_harm_less()):
                                if (collidet_enemy.isbossed()):
                                    boss_num -= 1
                                collidet_enemy.crashed()
                                p.crashed()

                    for b in bomb:
                        if (b.bombed):
                            collidet_enemy = collidet.get_collidet_sg_area(b, enemys, b.kill_R)
                            collidet_eshot = collidet.get_collidet_sg_area(b, enemy_shots, b.kill_R)
                        #and block will be added here...
                        for ce in collidet_enemy:
                            ce.destoried(b)
                            if (not ce.isalived() and ce.isboss):
                                boss_num -= 1

                        for ceshot in collidet_eshot:
                            ceshot.kill()

                    for shot in shots:
                        collidet_enemy = collidet.get_collidet_sg(shot, enemys)
                        if (collidet_enemy):
                            collidet_enemy.destoried(shot)
                            shot.kill()
                            if (not collidet_enemy.isalived() and collidet_enemy.isboss):
                                boss_num -= 1
                            flame.flame_spot(shot.rect.center)
                            #shot flame...
                    
                    for eshot in enemy_shots:
                        collidet_player = collidet.get_collidet_sg(eshot, players)
                        if (collidet_player):
                            if (not collidet_player.is_harm_less()):
                                collidet_player.destoried(eshot)
                                eshot.kill()
                                flame.flame_spot(eshot.rect.center)
                                #shot flame...
                    
                    for s in stars:
                        s.draw(self.screen)

                    for e in exps:
                        e.draw(self.screen)
                        
                    for eshot in enemy_shots:
                        eshot.draw(self.screen)

                    for shot in shots:
                        shot.draw(self.screen)

                    for b in bomb:
                        b.draw(self.screen)

                    for e in enemys:
                        e.draw(self.screen)

                    for g in gunfires:
                        g.draw(self.screen)

                    for p in players:
                        p.draw(self.screen)

                if (start_time % 2):
                    message.show(self.screen)

                if (start_time <= 10):
                    message.update("FIGHT")

                    pass

                somebody_alive = len(players)

#                print "sba: ", self.somebody_alive, "bn: ", self.boss_num

#                somebody_alive

                if (somebody_alive and not boss_num):
                    message.update("MISSION COMPLETE")
                    if (ending_time):
                        message.show(self.screen)
                    ending_time -= 1

                elif (not somebody_alive):
                    message.update("MISSION FAILED")
                    if (ending_time):
                        message.show(self.screen)
                    ending_time -= 1

                else:
                    pass

                pygame.display.flip()

            for p in players:
                p.reset_pos()
            
#            break
